11. Command Line Tutorial, Part I — Navigating Directories

Command Line Tutorial, Part I — Navigating Directories

If you have opened up the Terminal application on Mac or the Git Bash application on Windows, you're ready to begin this command line tutorial.

Tutorial Overview

In this tutorial, you’ll learn how to use the command line to navigate through files and folders, to create, open, move, and remove files and folders, and to run Python files.

Let’s start by learning how to navigate a computer’s file system. Take a moment to think about how you navigate your computer using a traditional mouse and graphical user interface (or GUI). The GUI shown below provides a visual interface for performing tasks on your computer.

Using a GUI to navigate files and folders on a computer.

Using a GUI to navigate files and folders on a computer.

Though the image above depicts a GUI on a Mac, the basic elements are the same on Windows. You may notice the following:

  • a window that shows a graphical representation of files and folders,
  • a navigation and file- or folder-opening process that involves mouse clicks,
  • a window that is updated upon click to display the contents of an opened folder, and
  • an option to move forward or backward through your navigation history.

A Comparison

Let’s use an example to compare the process of file- and folder-navigation using a GUI and using the command line. For this example, consider the sample file structure shown below.

Sample file structure.

Sample file structure.

To follow along with the task in this section, complete the following steps on your own computer:

  1. In your Desktop folder, create a folder named TopSecret.
  2. Inside this TopSecret folder, create another folder with the name Notes.
  3. Also inside the TopSecret folder, create an additional folder named Photos.
  4. Finally, in the Photos folder, add an image (any image will do), and name it adorable.jpg.

Our task in this example is to navigate to the Desktop, open the TopSecret folder, open the Photos folder, and open (to view) the adorable.jpg image.

Using a GUI, the navigation would resemble the one shown below:

Navigating our sample file structure through a GUI.

Navigating our sample file structure through a GUI.

Using the command line, the navigation would represent a different kind of interaction with the computer. Before we walk through the navigation process, you’ll need to become familiar with a handful of new terms and commands.

We’ll start by defining a directory. A directory is simply another name for a folder. When we’re working at the command line, we will refer to folders as directories.

A computer’s files and folders are structured like a tree. At the beginning is a root directory that ultimately branches out to many other folders (each with the potential to contain more folders and files). What we’re doing when we navigate our computer’s file system is effectively walking up and down certain branches of this figurative tree structure. When we enter a command line interface, we should think of ourselves as being present at a particular location on the computer — meaning that we’re currently inside some directory. By default, when we open the shell, we’ll be starting at the Home folder on our computer, indicated by a tilde (~) symbol.

We’ll notice in the shell that a cursor appears after a dollar sign ($). This is where we will enter commands.

The pwd command

The first command that will be useful for us is called pwd, which stands for “print working directory.” When we type this command and hit the RETURN or ENTER key on our keyboards, the shell will respond by outputting what’s called an absolute path to where we are in the computer’s file structure system. The printed path in the example below — /Users/udacity — contains a series of folder names separated by slashes (/), giving us the path from our computer’s root directory to our current location.

Using the `pwd` command.

Using the pwd command.

Note: By default, when we open the shell, we’ll be starting in our computer’s Home directory, displayed in abbreviated form by a tilde (~) symbol in the command prompt. The path to your own computer's home directory may differ depending on your username and operating system.

The ls command

To see the contents of a directory, we can use the ls (or “list”) command, as shown below.

Using the `ls` command.

Using the ls command.

Note: If you want to see all files in a directory (including hidden ones), you can add a flag — ls -a — to list “all” of the contents present. Hidden files will appear with a . preceding their names.

The open command

If you want to open a file or a directory, you can do so by using the open command on Mac. Note that if you are using Git Bash on Windows (per the previous installation instructions), you will most likely have to use the start command instead of open. Finally, Ubuntu users will need to use the xdg-open command.

To use the open command, you type open, followed by a space, followed by the name of the file or directory you wish to open. For example, if you’re starting out at your Home directory, and you see that it contains a Downloads directory, you can then open that Downloads directory by typing the following:

~ $ open Downloads

Opening the Downloads directory will bring up a window displaying (through a GUI) the contents of that directory.

Note: When we show you a new command, the example will sometimes include the shell prompt — the ~ $ in the example above. You don't type these characters; the shell displays them. So in the above example, you would type open Downloads, not ~ $ open Downloads.

Using the `open` command.

Using the open command.

Note: To be efficient, you can use the TAB key on your keyboard to autocomplete file and directory names that reside in your current (or “working”) directory. You may notice that autocompleting a directory name will add a trailing slash (/).

To open the current (working) directory, you can type the following command:

~ $ open .

The . in this context indicates your current (working) directory.

The cd command

Recall that when you open the shell, you start out at your computer’s Home directory, abbreviated in the prompt as ~. If you want to move from your Home directory into another, you can use the cd (or “change directory”) command.

To move into a different directory, you provide that new directory’s name as follows:

~ $ cd Desktop

The command above will move you from the Home directory into the Desktop directory, as shown below.

Using the `cd` command to access the `Desktop` directory.

Using the cd command to access the Desktop directory.

Notice that, when our current (working) directory changed from the Home (or ~) directory to the Desktop directory, the prompt text also changed from ~ $ to ~/Desktop $ . That’s because the text before the $ in the prompt is, by default, set up to display an absolute path to your current location in the computer’s file structure. This can be a helpful reminder of where you are if you’re ever traversing deep into the computer’s file structure. (Alternatively, you can always use the pwd command to print out your working directory!)

Once you’ve changed directories, you can easily access files and folders contained in that new directory. Notice in the image below that the ls command now displays the contents of the Desktop rather than the contents of the Home directory.

Using the `ls` command to list the contents of the new working directory (`Desktop`).

Using the ls command to list the contents of the new working directory (Desktop).

Just as we can move deeper into our computer’s file structure, we can also move back up to a higher-level folder as follows:

~/Desktop $ cd ..

Using .. indicates the parent directory, or the one above our working directory.

Using the `cd ..` command to move up to our current location's parent directory.

Using the cd .. command to move up to our current location's parent directory.

Finally, no matter where we are, if we simply type in the cd command without any destination directory, we’ll just be taken to our Home folder.

Now that you’ve learned the pwd, ls, open, and cd commands, you will be able to navigate directories on your computer from the command line.

Let’s revisit the comparison we set up at the beginning of this tutorial — navigating a sample file structure using a GUI versus navigating that structure using the command line.

Navigating through the GUI involved the following steps:

  • navigating to the “Desktop” folder,
  • opening the “TopSecret” folder,
  • opening the “Photos” folder, and
  • opening (or viewing) the “adorable.jpg” image.

In contrast, there are many ways to navigate to the same destination through the command line. One approach would be to move through the computer’s file structure (starting at the Home directory) until we reach and open the adorable.jpg file. You could use the following series of commands to do so (shown with outputs in the image below):

~ $ cd Desktop
~/Desktop $ cd TopSecret
~/Desktop/TopSecret $ ls
~/Desktop/TopSecret $ cd Photos
~/Desktop/TopSecret/Photos $ ls
~Desktop/TopSecret/Photos $ open adorable.jpg

It turns out that you can arrive at the same destination with fewer commands if you “chain” directory paths together. For instance, you can navigate straight to the Photos directory with a single command and then open the adorable.jpg file from your working directory:

~ $ cd Desktop/TopSecret/Photos
~/Desktop/TopSecret/Photos $ open adorable.jpg

Being able to use the command line to navigate your computer is a very useful skill to develop. On the next page, you’ll complete this command line tutorial by learning how to create and remove files and directories.